﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using XCommunity.Caching;
using System.Collections;

namespace XCommunity.Metas
{
  public class MetaData : IWriteBackCacheValue
  {

    private Hashtable _values;



    private int _dataId;

    /// <summary>
    /// 获取此元数据在数据库中的标识号
    /// </summary>
    protected int DataID
    {
      get { return _dataId; }
    }

    /// <summary>
    /// 获取元数据服务
    /// </summary>
    protected MetaService Service
    {
      get { throw new NotImplementedException(); }
    }

    /// <summary>
    /// 获取一个字典，它包含了此元数据的所有属性
    /// </summary>
    protected IDictionary<string, MetaProperty> Properties
    {
      get { throw new NotImplementedException(); }
    }


    /// <summary>
    /// 获取所有尚未提交的被修改的属性和值
    /// </summary>
    protected Dictionary<MetaProperty, object> ChangedProperties
    {
      get { throw new NotImplementedException(); }
    }

    /// <summary>
    /// 提交对此元数据的所有修改
    /// </summary>
    protected void SubmitChanges()
    {

    }

    internal void PrepareProperties()
    {
      Type type = GetType();

      foreach ( PropertyInfo property in type.GetProperties() )
      {
        var attributes = property.GetCustomAttributes( typeof( MetaPropertyAttribute ), false );

        MetaProperty metaProperty;

        if ( attributes.Length != 0 )
          metaProperty = ( (MetaPropertyAttribute) attributes[0] ).CreateMetaProberty();
        else
          metaProperty = new MetaProperty( property.Name );

      }
    }

    /// <summary>
    /// 获取指定属性的值
    /// </summary>
    /// <param name="propertyName">属性名</param>
    /// <returns>属性值</returns>
    protected object this[string propertyName]
    {
      get
      {
        if ( propertyName == null )
          throw new ArgumentNullException( "propertyName" );


        MetaProperty property = Properties[propertyName];
        if ( property == null )
          return null;

        return this[property];
      }
      set
      {
        if ( propertyName == null )
          throw new ArgumentNullException( "propertyName" );


        MetaProperty property = Properties[propertyName];
        if ( property == null )
          throw new ArgumentException( "找不到要设置的属性", "propertyName" );

        this[property] = value;
      }
    }

    /// <summary>
    /// 获取指定属性的值
    /// </summary>
    /// <param name="property">属性对象</param>
    /// <returns>属性值</returns>
    protected object this[MetaProperty property]
    {
      get
      {
        if ( property == null )
          throw new ArgumentNullException( "property" );

        lock ( _values.SyncRoot )
        {
          return _values[property];
        }
      }
      set { throw new NotImplementedException(); }
    }


    #region IWriteBackCacheValue 成员

    bool IWriteBackCacheValue.WriteBack()
    {
      Service.WriteMeta( ChangedProperties );
      return true;
    }

    #endregion

  }
}
